package com.jessechanrui.template.common.template.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jessechanrui.template.common.template.auth.application.assembler.UserResponseAssembler;
import com.jessechanrui.template.common.template.auth.application.assembler.UserUpdateRequestAssembler;
import com.jessechanrui.template.common.template.auth.application.dto.req.UpdateUserPasswordRequest;
import com.jessechanrui.template.common.template.auth.application.dto.req.UserUpdateRequest;
import com.jessechanrui.template.common.template.auth.application.dto.rsp.UserResponseDTO;
import com.jessechanrui.template.common.template.auth.application.service.UserService;
import com.jessechanrui.template.common.template.auth.domain.User;
import com.jessechanrui.template.core.dto.ResponseResult;
import com.jessechanrui.template.core.entity.BasePageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author chenrui
 * @version 1.0
 * @description 用户管理
 * @date 2025/4/24
 */
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserResponseAssembler userResponseAssembler;

    @Autowired
    private UserUpdateRequestAssembler userUpdateRequestAssembler;

    @Operation(summary = "用户列表(分页)")
    @GetMapping(value = "/listByPage", produces= MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<IPage<User>> ruleListByPage(@Valid BasePageRequest request) {
        return ResponseResult.of(userService.listByPage(request));
    }


    @Operation(summary = "修改用户（仅可修改用户的gerrit的账号与密码）")
    @PutMapping(value = "/modify", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult modify(@Valid @RequestBody UserUpdateRequest user) {
        userService.modify(userUpdateRequestAssembler.toEntity(user));
        return ResponseResult.buildSuccess();
    }

    @Operation(summary = "修改用户密码")
    @PutMapping(value = "/modify/password", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult updatePassword(@Valid @RequestBody UpdateUserPasswordRequest request) {
        userService.updatePassword(request);
        return ResponseResult.buildSuccess();
    }

    @Operation(summary = "同步用户")
    @GetMapping(value = "/synchronous", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult synchronous() {
        userService.synchronous();
        return ResponseResult.buildSuccess();
    }

    @Operation(summary = "用户详情")
    @GetMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<UserResponseDTO> detail(@Parameter(description = "用户名", required = true) @RequestParam String userName) {
        return ResponseResult.of(userResponseAssembler.toDTO(userService.detail(userName)));
    }

}
